package com.exp.practice.workbench.service.teacher.impl;

import com.exp.practice.base.exception.MyException;
import com.exp.practice.workbench.dao.*;
import com.exp.practice.workbench.domain.Examination;
import com.exp.practice.workbench.domain.ExaminationQuestionRelation;
import com.exp.practice.workbench.domain.Question;
import com.exp.practice.workbench.domain.Student;
import com.exp.practice.workbench.service.teacher.TeacherMakeTestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherMakeTestServiceImpl implements TeacherMakeTestService {

    @Resource
    private QuestionDao questionDao;


    @Resource
    private ExaminationDao examinationDao;

    @Resource
    private ExaminationQuestionRelationDao examinationQuestionRelationDao;

    @Resource
    private StudentDao studentDao;

    @Resource
    private ExaminationStudentRelationDao examinationStudentRelationDao;

    @Override
    public List<Question> getQuestions(String pageNo, String pageSize, String[] qid)  {
        int pageNos=Integer.valueOf(pageNo);
        int pageSizes=Integer.valueOf(pageSize);
        //记录略过的记录条数
        int skipCount=(pageNos-1)*pageSizes;
        //得到所选题目
        List<Question> qList=questionDao.getQuestions(skipCount,pageSizes,qid);
        return qList;
    }

    @Override
    public void addExamination(Examination examination, String[] qid) throws MyException {
        //增加一个试卷记录
        int count1=examinationDao.addOneExamination(examination);
        if(count1!=1){
            throw new MyException("生成试卷失败");
        }
        //增加试卷和题目关系记录
        String eid = examination.getEid();
        int count2=examinationQuestionRelationDao.add(eid,qid);
        if(count2!=qid.length){
            throw new MyException("生成试卷失败");
        }
    }

    @Override
    public int getTotal(Examination examination) {
        int total=examinationDao.getTotal(examination);
        return total;
    }

    @Override
    public List<Examination> getExaminationList(String pageNo, String pageSize, Examination examination) {
        int pageNos=Integer.valueOf(pageNo);
        int pageSizes=Integer.valueOf(pageSize);
        //记录略过的记录条数
        int skipCount=(pageNos-1)*pageSizes;
        String eName=examination.geteName();
        String teacherId = examination.getTeacherId();
        String startTime = examination.getStartTime();
        String endTime = examination.getEndTime();
        String published = examination.getPublished();
        List<Examination> list=examinationDao.getExaminationList(skipCount,pageSizes,eName,teacherId,startTime,endTime,published);
        return list;
    }

    @Override
    public void publish(String eid) throws MyException {
        int count=examinationDao.publish(eid);
        if(count!=1){
            throw new MyException("发布失败");
        }
        String[] stuIds=studentDao.getIds();
        if(stuIds==null){
            throw new MyException("发布失败");
        }
        int count2=examinationStudentRelationDao.add(stuIds,eid);
        if(stuIds.length!=count2){
            throw new MyException("发布失败");
        }
    }

    @Override
    public int delete(String[] eid) throws MyException {
        String []ids=examinationStudentRelationDao.isExists(eid);
        //所选试卷中有已发布试卷
        if(ids.length!=0){
            throw new MyException("所选试卷中有已发布试卷，不能删除");
        }
        int count=examinationDao.delete(eid);
        if(count!=eid.length){
            throw new MyException("删除失败");
        }
        return count;
    }

    @Override
    public Map<String, Object> getConcreteExamination(String eid) {
        Map<String,Object> map=new HashMap<>();
        //获得试卷记录
        Examination examination=examinationDao.getExaminationById(eid);
        //获得题目记录
        String[]qids=examinationQuestionRelationDao.getIds(eid);

        List<Question> list=questionDao.getQuestions(0,qids.length,qids);
       //将结果加入map中
        map.put("examination",examination);
        map.put("questionList",list);
        return map;
    }
}
